//
//  TodoWarnAllViewController.m
//  Rail
//
//  Created by hychen on 2018/2/28.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "TodoWarnAllViewController.h"
#import "InformationTableViewCell.h"
#import "WarnDetailViewController.h"

@interface TodoWarnAllViewController ()

@end

@implementation TodoWarnAllViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView.mj_header beginRefreshing];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)refreshData {
    BMWeakSelf
    self.isOpenFooter = NO;
    self.currentPage = 1;
    [HttpRequest loadWarnListDataWithStatus:@"" alertType:@"" eventLevel:@"" eventId:self.eventModel.eventid pageNo:[@(self.currentPage) stringValue] success:^(NSArray *object) {
        if (object.count > 9) {
            weakSelf.isOpenFooter = YES;
        }
        weakSelf.dataArray = [NSMutableArray arrayWithArray:object?:@[]];
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_header endRefreshing];
    } failure:^(NSString *code, NSString *msg) {
        [weakSelf.tableView.mj_header endRefreshing];
        Show_Msg_Error(msg)
    }];
}

- (void)loadMoreData {
    self.currentPage++;
    BMWeakSelf
    [HttpRequest loadWarnListDataWithStatus:@"" alertType:@"" eventLevel:@"" eventId:self.eventModel.eventid pageNo:[@(self.currentPage) stringValue] success:^(NSArray *object) {
        if ([object isKindOfClass:[NSArray class]] && object.count > 0) {
            [weakSelf.dataArray addObjectsFromArray:object];
            [weakSelf.tableView.mj_footer endRefreshing];
            [weakSelf.tableView reloadData];
        }
        else {
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    } failure:^(NSString *code, NSString *msg) {
        Show_Msg_Error(msg)
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WarnDetailModel *model = [self.dataArray objectAtIndex:indexPath.row];
    InformationTableViewCell *cell = [InformationTableViewCell cellWithTableView:tableView class:[InformationTableViewCell class]];
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WarnDetailModel *model = [self.dataArray objectAtIndex:indexPath.row];
    WarnDetailViewController *vc = [WarnDetailViewController new];
    vc.model = model;
    BMWeakSelf
    [vc setBlockProcessedWarn:^{
        [weakSelf.tableView.mj_header beginRefreshing];
    }];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
